package com.estar.assistsurvy.activity;

import java.util.List;

import com.estar.assistsurvy.R;
import com.estar.assistsurvy.adapter.BalanceListviewAdapter;
import com.estar.assistsurvy.entity.EsChangeDetailInfo;
import com.estar.assistsurvy.entity.RequestWalletChange;
import com.estar.assistsurvy.entity.ResponseChangeDetail;
import com.estar.assistsurvy.thread.WalletDetailThread;
import com.estar.assistsurvy.utils.ExitUtils;
import com.estar.assistsurvy.utils.MightypleUtil;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

public class BalanceActivity extends BaseActivity implements OnClickListener{
	private TextView returnBack,balance;
	//	private TextView notAccount,income;
	private Button withdraw;
	private ListView balanceList;
	private Intent intent;
	private String amount;
	private BalanceListviewAdapter adapter;
	private SharedPreferences prefer;
	private String uuid,deviceId;
	private List<EsChangeDetailInfo> esChangeDetailInfoList;
	private Handler handler=new Handler(){
		public void handleMessage(android.os.Message msg) {
			switch (msg.what) {
			case -1:
				showShortToast((String) msg.obj);
				break;

			case 0:
				ResponseChangeDetail changeDetail=new ResponseChangeDetail();
				changeDetail=(ResponseChangeDetail) msg.obj;
				
				if(changeDetail.getCode().equals("1")){
					esChangeDetailInfoList=changeDetail.getEsChangeDetailInfoList();
					adapter=new BalanceListviewAdapter(esChangeDetailInfoList, BalanceActivity.this);
					balanceList.setAdapter(adapter);
					if(esChangeDetailInfoList.size()==0||esChangeDetailInfoList==null){
						showShortToast("当前明细列表为空!");
					}
				}else if(changeDetail.getCode().equals("0")){
					showShortToast(changeDetail.getMessages());
				}else if(changeDetail.getCode().equals("2")){
					ExitUtils.Unlogin(BalanceActivity.this, changeDetail.getMessages());
				}
				break;

			default:
				break;
			}
		};
	};
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_balance);
		initView();
	}

	private void initView(){
		returnBack=find(R.id.returnBack);
		returnBack.setOnClickListener(this);
		balance=find(R.id.balance);
		//		notAccount=find(R.id.notAccount);
		//		income=find(R.id.income);
		withdraw=find(R.id.withdraw);
		withdraw.setOnClickListener(this);
		balanceList=find(R.id.balanceList);
		
		intent=getIntent();
		amount=intent.getStringExtra("amount");
		balance.setText(amount);
		prefer=getSharedPreferences("user", MODE_PRIVATE);
		uuid=prefer.getString("uuid", "");
		deviceId=MightypleUtil.getDeviceId(this);
		RequestWalletChange walletDetail=new RequestWalletChange(deviceId,uuid);
		new WalletDetailThread(this, handler, walletDetail).start();
	}


	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.returnBack:
			finish();
			break;

		case R.id.withdraw:
			intent=new Intent(this, WithdrawActivity.class);
			intent.putExtra("amount", amount);
			startActivity(intent);
			break;

		default:
			break;
		}
	}
}
